<form id="<?php echo $uniqid;?>" method="post" style="padding: 8px;">
    <table border="0" width="100%" cellspacing="1" cellpadding="5" class="formtable">
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">任务名称：</td>
            <td width="80%"><input type="text" style="width:400px;" v-model="params.name"
                                   class="forminput inputstyle textbox-text validatebox-text textbox-prompt easyui-validatebox"
                                   placeholder="请输入任务名称.." required="true"></td>
        </tr>
        <tr>
            <td class="tlabel">可领取任务等级：</td>
            <td width="80%">
                <label v-for="(vo, index) in levels" style="min-width: 100px; display: inline-block">
                    <input  type="checkbox" class="textbox-prompt" :value="vo.level" v-model="task_levels">{{ vo.name }}</label>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">可领取任务用户ID：</td>
            <td width="80%">
                <div class="display-flex" style="width: 560px;">
                    <el-input type="textarea" v-model="params.special_ids" placeholder="逗号间隔ID" :rows="6" width="560px" v-enter-number></el-input>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">任务时间：</td>
            <td width="80%">
                <input id="taskeditStartOn" style="width:152px" type="text" class="easyui-datetimebox"> </input>
                至
                <input id="taskeditEndOn" style="width:152px" type="text" class="easyui-datetimebox"> </input>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">任务持续天数：</td>
            <td width="80%">
                <div class="display-flex" style="width: 320px; height: 40px;">
                    <el-input v-model="params.task_duration" size="small" type="number" width="200px" v-enter-number>
                        <template slot="append">天</template>
                    </el-input>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">奖励持续天数：</td>
            <td width="80%">
                <div class="display-flex" style="width: 320px; height: 40px;">
                    <el-input v-model="params.reward_duration" size="small" type="number" width="200px" v-enter-number>
                        <template slot="append">天</template>
                    </el-input>
                </div>
            </td>
        </tr>
        <tr>
            <td class="tlabel">任务状态：</td>
            <td width="80%"><label>
                <input name="status" type="checkbox" class="textbox-prompt" v-model="params.status">开启</label>
            </td>
        </tr>
    </table>

    <table border="0" width="100%" cellspacing="1" cellpadding="5" class="formtable">
        <tr bgcolor="#FFFFFF">
            <td><b>任务完成规则</b></td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_consume.flag">
                    <el-checkbox true-label="1" false-label="0">新增团队业绩金额满
                        <el-input v-model="team_consume.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="total_consume.flag">
                    <el-checkbox true-label="1" false-label="0">新增自购订单金额满
                        <el-input v-model="total_consume.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child.flag">
                    <el-checkbox true-label="1" false-label="0">新增团队人数满
                        <el-input v-model="team_child.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_direct.flag">
                    <el-checkbox true-label="1" false-label="0">新增直推客户满
                        <el-input v-model="team_child_direct.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_agent_direct.flag">
                    <el-checkbox true-label="1" false-label="0">新增直推代理商满
                        <el-input v-model="team_child_agent_direct.amount" size="mini" v-enter-number
                                  style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_agent_all.flag">
                    <el-checkbox true-label="1" false-label="0">新增团队代理商满
                        <el-input v-model="team_child_agent_all.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>

        <tr bgcolor="#FFFFFF">
            <td>
                <div v-for="(vo,index) in team_child_agent">
                    <el-checkbox-group v-model="vo.flag">
                        <el-checkbox true-label="1" false-label="0">新增团队-{{vo.name}}满
                            <el-input v-model="vo.amount" size="mini" v-enter-number style="width: 160px"
                                      @input="inputChange">
                                <template slot="append">人</template>
                            </el-input>
                        </el-checkbox>
                    </el-checkbox-group>
                </div>
            </td>
        </tr>
    </table>

    <table border="0" width="100%" cellspacing="1" cellpadding="5" class="formtable">
        <tr bgcolor="#FFFFFF">
            <td><b>任务奖励规则</b></td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <div v-for="(vo,index) in reward_rules" style="display: flex; align-content: center; padding-top: 10px; padding-bottom: 10px;">
                    <div style="line-height: 28px; width: 120px; text-align: right;">{{vo.name}}可得奖励</div>
                    <div v-for="(vo1,index1) in vo.awards" style="display: flex; justify-content: center; align-content: center; padding-left: 20px; padding-right: 20px;">
                        <el-input size="mini" v-enter-number style="width: 160px"
                                  v-model="vo1.num"
                                  @input="inputChange">
                            <template slot="append">个 {{vo1.name}}</template>
                        </el-input>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</form>

<script>
    var taskeditVm = new Vue({
        el: '#<?php echo $uniqid;?>',
        data: {
            params: JSON.parse('<?php echo addslashes($dataset);?>'),
            levels: JSON.parse('<?php echo addslashes($levels);?>'),
            award: JSON.parse('<?php echo addslashes($award);?>'),
            team_consume: {flag: 0, amount: ''},
            total_consume: {flag: 0, amount: ''},
            team_child: {flag: 0, amount: ''},
            team_child_direct: {flag: 0, amount: ''},
            team_child_agent_all: {flag: 0, amount: ''},
            team_child_agent_direct: {flag: 0, amount: ''},
            team_child_agent: [],
            team_child_line: {flag: 0,num: '',level: '',amount: ''},
            reward_rules: [],
            task_levels: [],
        },
        created() {
            if(this.params.finish_rules) {
                this.params.finish_rules = JSON.parse(this.params.finish_rules);
            }
            this.task_levels = this.params.task_levels ? this.params.task_levels.split(',') : [];
            this.team_consume = this.params.finish_rules.team_consume ? this.params.finish_rules.team_consume : {flag: 0, amount: ''};
            this.total_consume = this.params.finish_rules.total_consume ? this.params.finish_rules.total_consume : {flag: 0, amount: ''};
            this.team_child = this.params.finish_rules.team_child ? this.params.finish_rules.team_child : {flag: 0, amount: ''};
            this.team_child_direct = this.params.finish_rules.team_child_direct ? this.params.finish_rules.team_child_direct : {flag: 0, amount: ''};
            this.team_child_agent_all = this.params.finish_rules.team_child_agent_all ? this.params.finish_rules.team_child_agent_all : {flag: 0, amount: ''};
            this.team_child_agent_direct = this.params.finish_rules.team_child_agent_direct ? this.params.finish_rules.team_child_agent_direct : {flag: 0, amount: ''};


            if(this.params.finish_rules.team_child_agent){
                this.team_child_agent = this.params.finish_rules.team_child_agent;
            }else {
                this.params.finish_rules = {};
                this.levels.forEach((it) => {
                    this.team_child_agent.push({
                        flag: 0,
                        name: it.name,
                        level: it.level,
                        amount: '',
                    });
                });
            }
            if(this.params.reward_rules){
                this.reward_rules = JSON.parse(this.params.reward_rules);
                this.reward_rules.forEach((it)=>{
                    if(!it.awards){
                        it.awards = [];
                    }
                });
            }else {
                this.params.reward_rules = {};
                let award = [];
                this.award.forEach((it)=>{
                    award.push({
                        id: it.id,
                        name: it.name,
                        num: ''
                    })
                })
                this.levels.forEach((it) => {
                    this.reward_rules.push({
                        name: it.name,
                        level: it.level,
                        awards: JSON.parse(JSON.stringify(award)),
                    });
                });
            }
        },
        mounted(){
            setTimeout(()=>{
                if(taskeditVm.params.start_on){
                    $('#taskeditStartOn').datetimebox('setValue', taskeditVm.params.start_on);
                }
                if(taskeditVm.params.end_on){
                    $('#taskeditEndOn').datetimebox('setValue', taskeditVm.params.end_on);
                }
            }, 300);
        },
        methods: {
            inputChange() {
                this.$forceUpdate();
            },
            checkRules() {
                if (this.team_consume.flag==="1") {
                    taskeditVm.params.finish_rules.team_consume = this.team_consume;
                }else{
                    delete taskeditVm.params.finish_rules.team_consume;
                }
                if (this.total_consume.flag==="1") {
                    taskeditVm.params.finish_rules.total_consume = this.total_consume;
                }else{
                    delete taskeditVm.params.finish_rules.total_consume;
                }
                if (this.team_child.flag==="1") {
                    taskeditVm.params.finish_rules.team_child = this.team_child;
                }else{
                    delete taskeditVm.params.finish_rules.team_child;
                }
                if (this.team_child_direct.flag==="1") {
                    taskeditVm.params.finish_rules.team_child_direct = this.team_child_direct;
                }else{
                    delete taskeditVm.params.finish_rules.team_child_direct;
                }
                if (this.team_child_agent_all.flag==="1") {
                    taskeditVm.params.finish_rules.team_child_agent_all = this.team_child_agent_all;
                }else{
                    delete taskeditVm.params.finish_rules.team_child_agent_all;
                }
                if (this.team_child_agent_direct.flag==="1") {
                    taskeditVm.params.finish_rules.team_child_agent_direct = this.team_child_agent_direct;
                }else{
                    delete taskeditVm.params.finish_rules.team_child_agent_direct;
                }
                let team_child_agent_arr = [];
                this.team_child_agent.forEach((it)=>{
                    team_child_agent_arr.push(it);
                })
                if(team_child_agent_arr.length>0) {
                    taskeditVm.params.finish_rules.team_child_agent = team_child_agent_arr;
                }else{
                    delete taskeditVm.params.finish_rules.team_child_agent;
                }
            },
            submit: function () {
                this.checkRules();
                taskeditVm.params.task_levels = taskeditVm.task_levels.join(',');
                taskeditVm.params.start_on = $('#taskeditStartOn').val();
                taskeditVm.params.end_on = $('#taskeditEndOn').val();
                taskeditVm.params.reward_rules = taskeditVm.reward_rules;
                let act = this.params.id ? 'edit' : 'add';
                $.post('/admin/task/' + act, taskeditVm.params, function (data) {
                    if (data.ret === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        taskReload();
                        $("#taskDlg").dialog('close');
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                });
            }
        }
    });
    $('#taskeditsubmit').click(function (data) {
        taskeditVm.submit();
    })
</script>
